var express = require("express");
const Blog = require("../models/blog_bean");
const blogController = require("../controllers/blog_controller");
const { loginChecker } = require("../middlewares/login_check");

var blogRouter = express.Router();
blogRouter.use(loginChecker);

// 增博客  http://localhost/blog/0             post
blogRouter.post("/0", (req, res) => {
    console.log(req.body.title, req.body.content);

    blogController.doAddBlog(
        new Blog(req.body.title, req.body.content),
        (ret) => res.json(ret),
        (err) => res.json(err)
    );
});

// 删博客  http://localhost/blog/:id           delete
blogRouter.delete("/:id", (req, res) => {
    console.log(req.params.id);

    blogController.doDeleteBlog(
        req.params.id * 1,
        (ret) => res.json(ret),
        (err) => res.json(err)
    );
});

// 改博客  http://localhost/blog/:id           put
blogRouter.put("/:id", (req, res) => {
    console.log(req.params.id, req.body.title, req.body.content);

    blogController.doUpdateBlog(
        new Blog(req.body.title, req.body.content, req.params.id * 1),
        (ret) => res.json(ret),
        (err) => res.json(err)
    );
});

// 查博客  http://localhost/blog/list/:page    get
blogRouter.get("/list/:page", (req, res) => {
    console.log(req.params.page);
    blogController.doGetBlogs(
        req.params.page * 1,
        (ret) => res.json(ret),
        (err) => res.json(err)
    );
});

module.exports = blogRouter;
